import re

from app.models import BMSInformation
from app.models.pcs_system import PowerSystem
from learn_test import transform2Hex2


def transformHex2(hex_string):
    # 将16进制字符串分割成4个字符一组
    hex_groups = [hex_string[i:i + 4] for i in range(0, len(hex_string), 4)]

    # 转换每个组为10进制数
    decimal_values = [int(group, 16) for group in hex_groups]
    return decimal_values


class DataParse:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(DataParse, cls).__new__(cls)
        return cls._instance

    def extractSN(self, msgTopic):
        pattern = r'\/(.*?)\/'
        match = re.search(pattern, msgTopic)

        if match:
            extracted_text = match.group(1)
            return extracted_text

    def parsePowerData(self, data):
        print("parse data")
        dataList = transformHex2(data)
        grid_voltage = dataList[0] * 0.1
        grid_voltage_harmonic_distortion = dataList[1] * 0.001
        grid_frequency = dataList[2] * 0.01
        output_voltage = dataList[3] * 0.1
        grid_current = dataList[4] * 0.01
        grid_active_power = dataList[5]
        grid_reactive_power = dataList[6]
        grid_apparent_power = dataList[7]
        grid_power_factor = dataList[8]
        ups_voltage = dataList[9]
        ups_current = dataList[10]
        ups_frequency = dataList[11]
        ups_active_power = dataList[12]
        ups_reactive_power = dataList[13]
        ups_apparent_power = dataList[14]
        ups_power_factor = dataList[15]
        bus_voltage = dataList[16]
        from app.models import PowerData
        return PowerData(grid_voltage=grid_voltage, grid_voltage_harmonic_distortion=grid_voltage_harmonic_distortion,
                         grid_frequency=grid_frequency, output_voltage=output_voltage, grid_current=grid_current,
                         grid_active_power=grid_active_power, grid_reactive_power=grid_reactive_power,
                         grid_apparent_power=grid_apparent_power, grid_power_factor=grid_power_factor,
                         ups_voltage=ups_voltage, ups_current=ups_current, ups_frequency=ups_frequency,
                         ups_active_power=ups_active_power, ups_reactive_power=ups_reactive_power,
                         ups_apparent_power=ups_apparent_power, ups_power_factor=ups_power_factor,
                         bus_voltage=bus_voltage, sn_id="")

    def parseBMSData(self, data):
        print("parse bms data")
        dataList = transformHex2(data)
        total_voltage = dataList[0]
        total_current = dataList[1]
        # 充放电状态
        charge_discharge_status = dataList[2]

        # 系统状态
        system_status = dataList[3]

        # 建议充电电压
        recommended_charge_voltage = dataList[4]

        # 建议放电电压
        recommended_discharge_voltage = dataList[5]

        # 最大允许充电电流
        max_charge_current = dataList[6]

        # 最大允许放电电流
        max_discharge_current = dataList[7]

        # SOC（State of Charge）
        soc = dataList[8]

        # SOH（State of Health）
        soh = dataList[9]

        # 温度
        temperature = dataList[10]

        # 系统故障状态
        system_fault_status = dataList[11]

        # 充电可用电量
        charge_available_energy = dataList[12]

        # 放电可用电量
        discharge_available_energy = dataList[13]

        # 告警信息字1
        alarm_info_1 = dataList[14]

        # 告警信息字2
        alarm_info_2 = dataList[15]

        # 告警信息字3
        alarm_info_3 = dataList[16]

        # 告警信息字4
        alarm_info_4 = dataList[17]

        # BMS版本
        bms_version = dataList[18]

        # 厂商信息1
        manufacturer_info_1 = dataList[19]

        # 厂商信息2
        manufacturer_info_2 = dataList[20]

        # 厂商信息3
        manufacturer_info_3 = dataList[21]

        return BMSInformation(sn_id="", total_voltage=total_voltage, total_current=total_current,
                              charge_discharge_status=charge_discharge_status, system_status=system_status,
                              recommended_charge_voltage=recommended_charge_voltage,
                              recommended_discharge_voltage=recommended_discharge_voltage,
                              max_charge_current=max_charge_current,
                              max_discharge_current=max_discharge_current,
                              soc=soc,
                              soh=soh,
                              temperature=temperature,
                              system_fault_status=system_fault_status,
                              charge_available_energy=charge_available_energy,
                              discharge_available_energy=discharge_available_energy,
                              alarm_info_1=alarm_info_1,
                              alarm_info_2=alarm_info_2,
                              alarm_info_3=alarm_info_3,
                              alarm_info_4=alarm_info_4,
                              bms_version=bms_version,
                              manufacturer_info_1=manufacturer_info_1,
                              manufacturer_info_2=manufacturer_info_2,
                              manufacturer_info_3=manufacturer_info_3)

    def parseSystemData(self, data):
        print("parse system data")
        dataList = transform2Hex2(data)

        print(dataList)
        pv1_output_voltage = dataList[0]

        pv1_output_current = dataList[1]
        # 充放电状态
        pv1_output_power = dataList[2]

        # 系统状态
        pv1_generation = dataList[3]

        # 建议充电电压
        pv2_output_voltage = dataList[4]

        # 建议放电电压
        pv2_output_current = dataList[5]

        # 最大允许充电电流
        pv2_output_power = dataList[6]

        # 最大允许放电电流
        pv2_generation = dataList[7]

        # SOC（State of Charge）
        battery_capacity_voltage = dataList[8]

        # SOH（State of Health）
        battery_side_voltage = dataList[9]

        # 温度
        battery_side_current = dataList[10]

        # 系统故障状态
        battery_side_inductor1_current = dataList[11]

        # 充电可用电量
        battery_side_inductor2_current = dataList[12]

        # 放电可用电量
        battery_side_charge_power = dataList[13]

        # 告警信息字1
        battery_side_discharge_power = dataList[14]

        # 告警信息字2
        battery_side_charge_quantity = dataList[15]

        # 告警信息字3
        battery_side_discharge_quantity = dataList[16]

        # 告警信息字4
        dc_component_output_current = dataList[17]

        # BMS版本
        dc_component_load_voltage = dataList[18]

        # 厂商信息1
        leakage_current = dataList[19]

        # 厂商信息2
        zero_ground_voltage = dataList[20]

        # 厂商信息3
        insulation_impedance_detection_voltage = dataList[21]

        relay_midpoint_voltage_1 = dataList[22]
        relay_midpoint_voltage_2 = dataList[23]

        return PowerSystem(sn_id="", pv1_output_voltage=pv1_output_voltage,
                           pv1_output_current=pv1_output_current,
                           pv1_output_power=pv1_output_power,
                           pv1_generation=pv1_generation,

                           # PV2
                           pv2_output_voltage=pv2_output_voltage,
                           pv2_output_current=pv2_output_current,
                           pv2_output_power=pv2_output_power,
                           pv2_generation=pv2_generation,

                           # 电池侧
                           battery_capacity_voltage=battery_capacity_voltage,
                           battery_side_voltage=battery_side_voltage,
                           battery_side_current=battery_side_current,
                           battery_side_inductor1_current=battery_side_inductor1_current,
                           battery_side_inductor2_current=battery_side_inductor2_current,
                           battery_side_charge_power=battery_side_charge_power,
                           battery_side_discharge_power=battery_side_discharge_power,
                           battery_side_charge_quantity=battery_side_charge_quantity,
                           battery_side_discharge_quantity=battery_side_discharge_quantity,

                           # 其他
                           dc_component_output_current=dc_component_output_current,
                           dc_component_load_voltage=dc_component_load_voltage,
                           leakage_current=leakage_current,
                           zero_ground_voltage=zero_ground_voltage,
                           insulation_impedance_detection_voltage=insulation_impedance_detection_voltage,
                           relay_midpoint_voltage_1=relay_midpoint_voltage_1,
                           relay_midpoint_voltage_2=relay_midpoint_voltage_2
                           )

    # 使用DataParse类
    # data_parser = DataParse()
    #
    # msgTopic = "device01/7CDFA1D0A90E/properties"
    # sn_id = data_parser.extractSN(msgTopic)
    # print(sn_id)
